import { Physics, Scene } from "phaser";
import type { Main } from "../Main";

enum BallType {
    Player,
    Snowman,
}

export class Snowball extends Physics.Arcade.Sprite {
    ballType: BallType;
    scene: Main = null;

    constructor(scene: Main, x: number, y: number, key: string, frame: string) {
        super(scene, x, y, key, frame);
        this.scene = scene;
        // 设置属性
        this.setScale(0.5);
        this.ballType = frame == "snowball2" ? BallType.Player : BallType.Snowman;
    }
    // 发射
    fire(x: number, y: number) {
        this.enableBody(true, x + 10, y - 44, true, true);
        // 玩家雪球
        if (this.ballType == BallType.Player) {
            this.setVelocityX(-600);
            this.setAccelerationX(-1400);
        }
        // 雪人雪球
        else {
            this.setVelocityX(200);
        }
    }
    preUpdate(time: number, delta: number) {
        super.preUpdate(time, delta);
        // 玩家雪球到头，销毁
        if (this.ballType == BallType.Player) {
            if (this.x <= -16) {
                this.disableBody(true, false);
            }
        }
        // 雪人雪球到头，游戏结束
        else {
            if (this.x >= 950) {
                this.scene.gameOver();
            }
        }
    }
}
